package com.trover.myapplication.ui.home

import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import com.trover.myapplication.R
import com.trover.myapplication.core.ui.BaseVMFragment
import com.trover.myapplication.core.util.toast
import kotlinx.android.synthetic.main.fragment_home.*
import org.koin.androidx.viewmodel.ext.android.getViewModel

class HomeFragment : BaseVMFragment<HomeViewModel>() {

    override fun initVM(): HomeViewModel = getViewModel()

    override fun startObserve() {
        mViewModel.apply {
            uiState.observe(viewLifecycleOwner, Observer { it ->
                it?.showSuccess.let {
                    text_home.text = it.toString()
                }

                it?.showError?.let { message ->
                    activity?.toast(if (message.isBlank()) "网络异常" else message)
                    text_home.text = message
                }

                it?.needLogin?.let {
                    if (it)
                        Navigation.findNavController(text_home).navigate(R.id.action_tab_to_login)
                }
            })
        }
    }

    override fun getLayoutResId(): Int = R.layout.fragment_home

    override fun initView() {

    }

    override fun initData() {
        mViewModel.getPlans()
    }
}